<?php echo $this->render('/public/header'); ?>
    <style>
        input {
            width: 100px
        }
    </style>
<?php use yii\helpers\Url; ?>
    <div id="main" class="col-xs-12">
        <div class="form-inline">
            <?php if ($user['role'] == 1 || $user['role'] == 2): ?>
                <button class="btn btn-success" onclick="Page.show()">添加记录</button>
            <?php endif; ?>
            <form method="get" action="">
                <div class="form-inline">
                    <div class="form-group">
                        <select class="form-control" name="express_id">
                            <?php $express_id = yii::$app->request->get('express_id') ? yii::$app->request->get('express_id') : "" ?>
                            <option value="0" <?php if ($express_id == 0) {
                                echo "selected";
                            } ?>>--请选择--
                            </option>
                            <?php foreach ($this->params['express_type'] as $k => $v) { ?>
                                <option <?php if ($express_id == $v['id']) {
                                    echo "selected";
                                } ?> value="<?php echo $v['id']; ?>"><?php echo $v['name']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <input placeholder="搜索开始时间" type="text" name="begin_time" class="form-control"
                               value="<?php echo yii::$app->request->get('begin_time'); ?>"
                               onclick="layui.laydate({elem: this})">
                    </div>
                    <div class="form-group">
                        <input placeholder="搜索结束时间" type="text" name="end_time" class="form-control"
                               value="<?php echo yii::$app->request->get('end_time'); ?>"
                               onclick="layui.laydate({elem: this})">
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="name">
                            <option value="0">选择业务员</option>
                            <?php foreach ($this->params['staff_list'] as $v): ?>
                                <option value="<?php echo $v['job_number']; ?>" <?php echo $v['job_number'] == yii::$app->request->get('name') ? 'selected' : ''; ?>><?php echo $v['name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <select name="type" class="form-control">
                            <option value="0">选择类型</option>
                            <option value="1" <?php echo yii::$app->request->get('type') == 1 ? 'selected' : '' ?>>大货
                            </option>
                            <option value="2" <?php echo yii::$app->request->get('type') == 2 ? 'selected' : '' ?>>样品费
                            </option>
                        </select>
                    </div>
                    <div class="form-group">
                        <input value="<?php echo yii::$app->request->get('customer_number'); ?>" type="text"
                               name="customer_number" class="form-control" placeholder="搜索客户编号"/>
                    </div>
                    <div class="form-group">
                        <input value="<?php echo yii::$app->request->get('customer_name'); ?>" type="text"
                               name="customer_name" class="form-control" placeholder="搜索客户"/>
                    </div>
                    <div class="form-group">
                        <input placeholder="搜索invoice" type="text" class="form-control" name="invoice"
                               value="<?php echo yii::$app->request->get('invoice'); ?>"/>
                    </div>
                    <div class="form-group">
                        <input placeholder="搜索单号" type="text" class="form-control" name="tradeno"
                               value="<?php echo yii::$app->request->get('tradeno'); ?>"/>
                    </div>
                    <div class="form-group">
                        <input placeholder="金额搜索" type="text" class="form-control" name="fee"
                               value="<?php echo yii::$app->request->get('fee'); ?>"/>
                    </div>
                    <div class="form-group">
                        <button class="layui-btn layui-btn-normal">搜索</button>
                    </div>
                    <div class="alert alert-danger" style="float:right;marign-left:10px">累计费用:
                        <?php echo $this->params['pay_all'] ? $this->params['pay_all'] : 0; ?>
                    </div>
                </div>
            </form>
        </div>
        <br/>
        <table class="table table-bordered">
            <thead>
            <tr>
                <th style="width:35px">id</th>
                <th style="width:70px">日期</th>
                <th style="width:50px">快递公司</th>
                <th style="width:60px">业务员名</th>
                <th style="width:60px">类型</th>
                <th style="width:50px">是否拆分</th>
                <th style="width:50px">客户编号</th>
                <th style="width:100px">客户全名</th>
                <th style="width:100px">invoice</th>
                <th style="width:50px">发往国家</th>
                <th style="width:50px">发件渠道</th>
                <th style="width:50px">单号</th>
                <th style="width:50px">转单号</th>
                <th style="width:50px">件数</th>
                <th style="width:50px">重量</th>
                <th style="width:50px">单价</th>
                <th style="width:80px">运费(财务)</th>
                <th style="width:140px">备注</th>
                <th style="width:40px">最后修改</th>
                <th style="width:50px">最后修改时间</th>
                <th style="width:160px">操作</th>
            </tr>
            </thead>
            <style>
                .table_input {
                    margin: 0px;
                    padding: 0px;
                    border: none;
                    width: 100%
                }
            </style>
            <?php if ($express_list): ?>
                <?php foreach ($express_list as $k => $v) : ?>
                    <tr>
                        <td><?php echo $v['id']; ?></td>
                        <td>
                            <?php echo $v['date']; ?>
                        </td>
                        <td>
                            <?php foreach ($this->params['express_type'] as $v2): ?>
                                <?php echo $v2['id'] == $v['express_id'] ? $v2['name'] : ''; ?>
                            <?php endforeach; ?>
                        </td>
                        <td>
                            <select class="table_input"
                                    onchange="update_by_column(<?php echo $v['id']; ?>,'name',this)">
                                <option value=0>--请选择--</option>
                                <?php foreach ($this->params['staff_list'] as $k => $v2): ?>
                                    <option <?php if ($v['name'] == $v2['job_number']) {
                                        echo "selected";
                                    } ?> value="<?php echo $v2['job_number']; ?>"><?php echo $v2['name']; ?></option>
                                <?php endforeach ?>
                            </select>
                        </td>
                        <td>
                            <select class="form-control" style="border: none;padding: 0"
                                    onchange="update_by_column(<?php echo $v['id']; ?>,'type',this)">
                                <option value="0">请选择</option>
                                <option value="1" <?php echo $v['type'] == 1 ? 'selected' : ''; ?>>大货</option>
                                <option value="2" <?php echo $v['type'] == 2 ? 'selected' : ''; ?>>样品费</option>
                            </select>
                        </td>
                        <td><?php echo $v['pid'] != $v['id'] ? '拆分项父' . $v['pid'] : ''; ?></td>
                        <td>
                            <input style="<?php echo $v['same'] ? '' : 'color:red'; ?>" type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'customer_number',this)"
                                   value="<?php echo $v['customer_number']; ?>"/>
                        </td>
                        <td>
                            <input style="<?php echo $v['same'] ? '' : 'color:red'; ?>" type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'customer_name',this)"
                                   value="<?php echo $v['customer_name']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'invoice_order',this)"
                                   value="<?php echo $v['invoice_order']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'country',this)"
                                   value="<?php echo $v['country']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'express_channel',this)"
                                   value="<?php echo $v['express_channel']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'tradeno',this)"
                                   value="<?php echo $v['tradeno']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'to_tradeno',this)"
                                   value="<?php echo $v['to_tradeno']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'num',this)"
                                   value="<?php echo $v['num']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'weight',this)"
                                   value="<?php echo $v['weight']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'per_kg',this)"
                                   value="<?php echo $v['per_kg']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   style="<?php echo $v['moneySame'] ? '' : 'color:red'; ?>"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'fee',this)"
                                   value="<?php echo $v['fee']; ?>"/>
                        </td>
                        <td>
                            <input type="text" class="table_input"
                                   onchange="update_by_column(<?php echo $v['id']; ?>,'remark',this)"
                                   value="<?php echo $v['remark']; ?>"/>
                        </td>
                        <td>
                            <?php foreach ($this->params['staff_list'] as $k => $v2): ?>
                                <?php if ($v['last_modify'] == $v2['job_number']): ?>
                                    <?php echo $v2['name']; ?>
                                <?php endif; ?>
                            <?php endforeach ?>
                        </td>
                        <td><?php echo $v['last_modify_time'] > 0 ? date('Y-m-d H:i:s', $v['last_modify_time']) : ''; ?></td>
                        <td>
                            <?php if ($this->params['show_fee']): ?>
                                <?php if ($v['split_status'] == 1): ?>
                                    <button onClick="splitExpress(<?php echo $v['id']; ?>)" class="btn btn-success"
                                            style="width:50px">拆分
                                    </button>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php if ($user['role'] == 1 || $user['role'] == 2): ?>
                                <button onclick="del_express(<?php echo $v['id']; ?>)" class="btn btn-danger">
                                    删除
                                </button>
                            <?php endif; ?>
                            <button onclick="lock_express(<?php echo $v['id']; ?>,<?php echo $v['lock'] == 2 ? '1' : '2'; ?>)"
                                    class="btn btn-primary">
                                <?php echo $v['lock'] == 2 ? '锁定' : '解锁'; ?>
                            </button>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </table>
        <div id="page" style="display: inline-block"></div>
        <span>共有<?php echo $this->params['page']['size']; ?>条记录</span>
        <script>
            var Page = {};
            layui.laypage({
                cont: 'page',
                pages: <?php echo $this->params['page']['page'];?>,
                curr: <?php echo $this->params['page']['curr'];?>,
                skin: '#5FB878',
                skip: true,
                jump: function (obj, first) {
                    if (!first) {
                        if (window.location.search) {
                            window.location.href = "<?php echo $this->params['page']['base_url'];?>&p=" + obj.curr;
                        } else {
                            window.location.href = "<?php echo $this->params['page']['base_url'];?>?p=" + obj.curr;
                        }

                    }
                }
            });

            function splitExpress(id) {
                const url = '/api/split-express?id=' + id;
                zeroModal.show({
                    url: url
                })
            }

            function lock_express(id, v) {
                var content = (v == 2) ? '确认解锁' : '确认锁定';
                layer.confirm(content, {}, function () {
                    var data = {};
                    data.id = id;
                    data.lock = v;
                    $.post('/table/express-lock', data, function (res) {
                        layer.alert(res.msg);
                        if (res.code == 1) {
                            setTimeout(window.location.reload(), 1000);
                        }
                    }, 'json')
                })
            }

            function del_express(v) {
                if (layer.confirm('确认删除', {}, function () {
                        var url = "<?php echo Url::to(['table/express_del']);?>"
                        $.post(url, {
                            id: v
                        }, function (res) {
                            if (res.code == 1) {
                                layer.confirm(res.msg, {}, function () {
                                    window.location.reload();
                                })
                            } else {
                                layer.alert(res.msg);
                            }
                        }, 'json')
                    })) ;
            }

            function update_by_column(id, column, e) {
                if (layer.confirm('确认修改', {}, function () {
                        var data = {
                            id: id,
                            column: column,
                            v: e.value
                        };
                        var url = "<?php echo Url::to(['table/update_express']);?>";
                        $.post(url, data, function (res) {
                            if (res.code == 1) {
                                layer.confirm(res.msg, {}, function () {
                                    window.location.reload();
                                })
                            } else {
                                layer.alert(res.msg);
                            }
                        }, 'json')
                    })) ;
            }

            Page.show = function () {
                zeroModal.show({
                    url: "/table/ajax_add_express"
                });
            }
        </script>
    </div>
<?php echo $this->render('/public/footer'); ?>